/**
 * Copyright 2008 floop.pl
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.sqlgen;

import java.util.Iterator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import org.eaicompiler.model.ScriptDefinition;
import org.eaicompiler.sqlgen.builders.EventaiScriptsSQLBuilder;
import org.eaicompiler.sqlgen.builders.LocalizedTextsSQLBuilder;
import org.eaicompiler.sqlgen.builders.MangosSQLBuilder;
import org.eaicompiler.sqlgen.builders.SummonsSQLBuilder;

/**
 * SQL generator that turns the script definition into SQL queries. 
 * @author floop.pl
 */
public class SqlGenerator {
	private boolean useDelete;
	private SortedMap<Integer, ScriptDefinition> creatures;
		
	@SuppressWarnings("unchecked")
	public SqlGenerator(List scriptDefinitions)
	{
		super();
		// Should we generate delete statements?
		this.useDelete = true;
		creatures = new TreeMap<Integer, ScriptDefinition>();
		Iterator it = scriptDefinitions.iterator();
		while(it.hasNext())
		{
			ScriptDefinition sd = (ScriptDefinition) it.next();
			Iterator<Integer> creatureId = sd.getCreatureFor().iterator();
			while(creatureId.hasNext())
			{
				creatures.put(creatureId.next(), sd);
			}
		}
	}

	public void setUseDelete(boolean useDelete) {
		this.useDelete = useDelete;
	}

	public String getLocalizedTextsSQL()
	{
		StringBuffer sql = new StringBuffer();
		LocalizedTextsSQLBuilder builder = new LocalizedTextsSQLBuilder(useDelete, creatures);
		sql.append(builder.buildSQLForCreatures());
		return sql.toString();
	}
	
	public String getEventsTextsSQL()
	{
		StringBuffer sql = new StringBuffer();
		EventaiScriptsSQLBuilder builder = new EventaiScriptsSQLBuilder(useDelete, creatures);
		sql.append(builder.buildSQLForCreatures());
		return sql.toString();
	}	

	public String getSummonsTextsSQL()
	{
		StringBuffer sql = new StringBuffer();
		SummonsSQLBuilder builder = new SummonsSQLBuilder(useDelete, creatures);
		sql.append(builder.buildSQLForCreatures());
		return sql.toString();
	}	
	
	public String getMangosSQL()
	{
		StringBuffer sql = new StringBuffer();
		MangosSQLBuilder builder = new MangosSQLBuilder(useDelete, creatures);
		sql.append(builder.buildSQLForCreatures());
		return sql.toString();		
	}

}
